package com.ltu.timkor6.gamesite.listings.client;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;
import com.ltu.timkor6.gamesite.shared.Result;

public class ListingsComposite extends CompositeAbstract
{
	private FlexTable table;
	
	public ListingsComposite(Listings listings)
	{
		// Title
		final Label title = new Label("Listings");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		table = new FlexTable();
		table.setStyleName("listings-table");
		table.getRowFormatter().setStyleName(0, "header");
		table.getColumnFormatter().setStyleName(0, "first-column");
		table.getColumnFormatter().setStyleName(1, "second-column");
		panel.add(table);
		
		initWidget(panel);
	}
	
	public void drawLists(List<Result> list)
	{
		int row = 0;
		
		table.setText(row, 0, "Game Title");
		table.setText(row, 1, "Turns");
		table.setText(row, 2, "Points");
		
		for(Result result : list)
		{
			row++;
			
			table.setText(row, 0, result.getGameTitle());
			table.setText(row, 1, String.valueOf(result.getTurns()));
			table.setText(row, 2, String.valueOf(result.getPoints()));
		}
	}
	
	public void drawListsNames(List<Result> list)
	{
		int row = 0;
		
		table.setText(row, 0, "Username");
		table.setText(row, 1, "Points");
		table.setText(row, 2, "Member since");
		
		for(Result result : list)
		{
			row++;
			
			String date = calculateDate(result.getDate());
			
			table.setText(row, 0, result.getUserName());
			table.setText(row, 1, String.valueOf(result.getPoints()));
			table.setText(row, 2, date + " " + result.getDate());
		}
	}

	private String calculateDate(int date)
	{
		String string = "";
		
		int year = 1970;
		int month = 1;
		int day = 1;
		
		int dayInSeconds = 86400;
		int yearInSeconds = 31536000;
		
		year += date / yearInSeconds;
		day += (date % yearInSeconds) / 365 / dayInSeconds;
		month += (day / 30);
		
		Window.alert(year + " - " + month + " - " + day);
		
		return "";
	}
	
	
}
